import requests
from PIL import Image
import pytesseract
import sys
import MySQLdb
import  time

def processImage(infile):
    try:
        im = Image.open(infile)
    except IOError:
        print
        "Cant load", infile
        sys.exit(1)
    i = 0
    mypalette = im.getpalette()
    try:
        while 1:
            im.putpalette(mypalette)
            new_im = Image.new("RGBA", im.size)
            new_im.paste(im)
            new_im.save('foo' + str(i) + '.png')
            i += 1
            im.seek(im.tell() + 1)
    except EOFError:
        pass  # end of sequence
ul = 'https://www.xmrc.com.cn/net/common/PageCounter.ashx?Cat=totalrecruit&v=1'
html = requests.get(ul)
file = open('./jobcnt.gif' , 'wb')
for chunk in html.iter_content(chunk_size=128):
    file.write(chunk)
file.flush()
gifFileName = './jobcnt.gif'
file.close()
processImage('./jobcnt.gif')

image = Image.open('./foo0.png')
text = pytesseract.image_to_string(image,lang='eng',config='--psm 6')
print(text)
print( time.time())

db = MySQLdb.connect(host='49.235.25.67',user='caiyu',passwd='12345678',db='background',charset='utf8')
sql = """ insert into xmjobcnt(timestamp,jobcnt) values ( from_unixtime('%s'),%s) """ % (int(time.time()),text)
print(sql)
cursor = db.cursor()
cursor.execute(sql)
db.commit()
cursor.close()


